import { _decorator, geometry, Label, easing, Tween, assetManager, Camera, Node, Color, math, Sprite, SystemEvent, LabelComponent, SkinnedMeshRenderer, Prefab, v3, Vec3, SkeletalAnimation, ProgressBar, tween } from 'cc';

import BaseView from '../../Script/game/BaseView';
import { Bullet } from '../../Script/config/BaseData';
import Tool from '../../Script/tool/Tool'
// import { luckyBox } from './luckyBox';
import GD from '../../Script/config/GD';
import GDef from '../../Script/config/GDef';
import TyqEventMgr from '../../Script/tyq3.x/tyq-event-mgr';
import SdkMgr from '../../Script/tyq3.x/SdkMgr';
const { bits } = math;
const { ccclass, property } = _decorator;

@ccclass('preBtnEgg')
export class preBtnEgg extends BaseView {
    @property({
        type: Node
    })
    nodeChild: Node = null;
    @property({
        type: Label
    })
    label: Label = null;
    private time: number = 0;
    private lastTime: number = 0;
    private stat: number = 0;
    onLoad() {
        let self = this;
        this.bindButton(this.node, () => {
            if (self.label.string == '领取') {
                self.addArm();
            }
            else {
                TyqEventMgr.ins.sendEvent('观看激励视频-龙蛋加速获得');
                SdkMgr.ins.showRewardAd("观看激励视频-龙蛋加速获得", () => {
                    TyqEventMgr.ins.sendEvent('观看完激励视频-龙蛋加速获得');
                    self.addArm();
                }, () => {
                }, () => {
                })
            }
        });
    }
    addArm() {
        console.error("addArm");
        let self = this;
        GD.removeEgg(self.jsonArm.Tid, self.time);
        self.emit(GDef.wdEvent.showFrameArm, 2, self.jsonArm.Tid);
        GDef.poolBtnEgg.put(self.node);
        self.emit(GDef.wdEvent.castDino);
        GD.addCastDinoAdCount();
        //更新按钮状态
        this.emit(GDef.wdEvent.updateCastDino);
        this.emit(GDef.wdEvent.updateCastHunter);
        this.emit(GDef.wdEvent.addCastDinoNum);
    }
    init(tid: number, time: number, parent: Node, node3D: Node, useAni: boolean = true) {
        this.lastTime = 0;
        this.jsonArm = null;
        this.stat = 0;
        this.label.string = '';
        this.jsonArm = jsonTables.getJsonTableObj("dino", tid);
        this.time = time;
        this.parent = parent;
        this.node3D = node3D;
        this.calTime();

        let self = this;
        if (useAni) {
            this.node.parent = GDef.getRoot();
            if (this.stat == 0) {
                if (this.node3D) {
                    Tool.setPos(this.nodeChild, this.node3D, v3(-this.node.position.x, -this.node.position.y, -this.node.position.z), v3(1, 1, 1));
                }
                this.stat = 1;
            }
            let targetPos = this.parent.parent.getWorldPosition().clone().subtract(GDef.getRoot().getWorldPosition().clone());
            let targetScale = GD.getEggList().length >= 3 ? 0.5 : 1;
            if (GD.getEggList().length > 3) {

                targetPos = v3(targetPos.x, targetPos.y - 68 - 2 * 115 - 50, targetPos.z);
                tween(this.nodeChild).set({ scale: v3(0, 0, 0) }).parallel(
                    tween().to(1, { position: targetPos }),
                    tween().to(1, { scale: v3(1, 1, 1) }),
                ).to(0.4, { scale: v3(0, 0, 0) }).call(() => {
                    self.nodeChild.setPosition(v3(0, 0, 0));
                    // self.parent.insertChild(self.node, 0);
                    self.node.parent = self.parent;
                }).start();
            }
            else {
                targetPos = v3(targetPos.x, targetPos.y - 68 - Math.min(3, GD.getEggList().length - 1) * 115, targetPos.z);
                tween(this.nodeChild).set({ scale: v3(0, 0, 0) }).parallel(
                    tween().to(1, { position: targetPos }),
                    tween().to(1, { scale: v3(1, 1, 1) }),
                ).call(() => {
                    self.nodeChild.setPosition(v3(0, 0, 0));
                    // self.parent.insertChild(self.node, 0);
                    self.node.parent = self.parent;
                }).start();
            }
        }
        else {
            self.node.parent = self.parent;
            // self.parent.insertChild(self.node, 0);
            self.nodeChild.setPosition(v3(0, 0, 0));
        }

    }
    calTime() {
        if (!this.jsonArm) return;
        let costt = Math.floor((new Date().getTime() - this.time) / 1000);
        let leftT = Math.max(0, parseInt(this.jsonArm.eggTime) - costt);
        this.label.string = (leftT == 0 ? "领取" : Tool.formatTime(leftT));
    }
    update(dt: number) {
        if (this.lastTime >= 1) {
            this.calTime();
            this.lastTime = 0;
            this.node3D = null;
        }
        this.lastTime += dt;
    }
}
